var date = null,dept=null;
$(function(){
	$(".datepicker").datepicker({
		dateFormat: 'yy-mm-dd',
	    onSelect:function(date,inst){
	    	dateSelect(date,inst);
	    }		
	
	});	
	$("#date").bind("click",function(){
		$(".datepicker").datepicker('show');
	});
	fillDepartments("depts");
});

function dateSelect(d,inst){
	date = d;
	checkResetable(date,dept);
}
/** 填充部门控件 */
function fillDepartments(deptId) {
	var depts = $("#"+deptId);
	$.getJSON('admin/department/department!bytype',
			{
		      "bean.id":"root",
		      "bean.departmentType":"Manufacture"		
			},
			
		function(data, textStatus, jqXHR) {

		$.each(data, function(i, item) {
			var ipt = $("<button type='button' class='btn btn-info' dept='"+item['id']+"'>"+item['name']+"</button>");
			ipt.bind("click",function(){
				departmentClicked($(this).attr("dept"),$(this).html());
			});
			depts.append(ipt);			
		});	
		
	});
}
function departmentClicked(d,text){
	dept = d;
	var $this =  $(window.event.srcElement);
	var sibs = $this.siblings("button");
	$.each(sibs,function(idx,item){
		$(item).removeClass("btn-warning");
		$(item).addClass("btn-info");
	});
	$this.removeClass("btn-info");
	$this.addClass("btn-warning");
	checkResetable(date,dept);
}

function checkResetable(date,dept){

	if(date==null || dept==null)return;
	$.post('reportreseted/report-reseted!toggle','bean.resetable=true&bean.department.id='+dept+'&bean.dueDate='+date,function(){
		doGet('reportreseted/report-reseted!pager');
	});
}
function toggleOne(dept,date){
	if(date==null || dept==null)return;
	$.post('reportreseted/report-reseted!toggle','bean.resetable=false&bean.department.id='+dept+'&bean.dueDate='+date,function(){
		doGet('reportreseted/report-reseted!pager');
	});
}